
/*

___________________________________________________________________________
Selection analysis includes the following items

_________
Main text

Figure 1: Share of new entrepreneurs by individual and parental income
	
Figure 2: Share of new incorporated entrepreneurs by individual and parental income and by parental entrepreneurship

Figure 3: Initial equity of new incorporated businesses by individual and parental income and by parental entrepreneurship

_________
Appendix

Table A2: Probability of starting an incorporated business: Comparing the relative importance of various predictors using dominance analysis

Figure A2: Share of new entrepreneurs by age

Figure A3: Share of new entrepreneurs by education

Figure A4: Share of highly-educated individuals by individual income

Figure A6: Share of new incorporated entrepreneurs by parental income using different income concepts

Figure A7: Share of new entrepreneurs by parental income: New firms vs. existing firms

*/









/*
________________________________________________________________________________
Main text
*/

/*
________________________________________________________________________________
Figure 1: Share of new entrepreneurs by individual and parental income
*/

* Individual

use ptile_dspi event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_dspi: gen ie_own_nobs = _N
collapse ie_own_nobs, by(ptile_dspi)
rename ptile_dspi rank
save "$results\03_selection\own_ie.dta", replace

use ptile_dspi event_uie if event_uie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_dspi: gen uie_own_nobs = _N
collapse uie_own_nobs, by(ptile_dspi)
rename ptile_dspi rank
save "$results\03_selection\own_uie.dta", replace

* Parental

use ptile_prnt event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_prnt: gen ie_prnt_nobs = _N
collapse ie_prnt_nobs, by(ptile_prnt)
rename ptile_prnt rank
save "$results\03_selection\prnt_ie.dta", replace

use ptile_prnt event_uie if event_uie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_prnt: gen uie_prnt_nobs = _N
collapse uie_prnt_nobs, by(ptile_prnt)
rename ptile_prnt rank
save "$results\03_selection\prnt_uie.dta", replace

* Combine

use "$results\03_selection\own_ie.dta", clear
merge 1:1 rank using "$results\03_selection\own_uie.dta"
drop _merge
merge 1:1 rank using "$results\03_selection\prnt_ie.dta"
drop _merge
merge 1:1 rank using "$results\03_selection\prnt_uie.dta"
drop _merge
erase "$results\03_selection\own_ie.dta"
erase "$results\03_selection\own_uie.dta"
erase "$results\03_selection\prnt_ie.dta"
erase "$results\03_selection\prnt_uie.dta"

foreach x in ie_own uie_own ie_prnt uie_prnt {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_own_nobs "Number of new incorporated entrepreneurs by income rank at t-1"
label var uie_own_nobs "Number of new unincorporated entrepreneurs by income rank at t-1"
label var ie_prnt_nobs "Number of new incorporated entrepreneurs by parental income rank"
label var uie_prnt_nobs "Number of new unincorporated entrepreneurs by parental income rank"
label var ie_own_shr "Share of new incorporated entrepreneurs by income rank at t-1"
label var uie_own_shr "Share of new unincorporated entrepreneurs by income rank at t-1"
label var ie_prnt_shr "Share of new incorporated entrepreneurs by parental income rank"
label var uie_prnt_shr "Share of new unincorporated entrepreneurs by parental income rank"
label var rank "Percentile rank, either at t-1 or parental"

save "$results\03_selection\fig1.dta", replace

/*
________________________________________________________________________________
Figure 2: Share of new incorporated entrepreneurs by individual and parental income and by parental entrepreneurship
*/

* Individual

use ptile_dspi event_ie prnt_entr if event_ie==-1 & prnt_entr==1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_dspi: gen ie_own_peyes_nobs = _N
collapse ie_own_peyes_nobs, by(ptile_dspi)
rename ptile_dspi rank
save "$results\03_selection\own_ie_peyes.dta", replace

use ptile_dspi event_ie prnt_entr if event_ie==-1 & prnt_entr==0 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_dspi: gen ie_own_peno_nobs = _N
collapse ie_own_peno_nobs, by(ptile_dspi)
rename ptile_dspi rank
save "$results\03_selection\own_ie_peno.dta", replace

* Parental

use ptile_prnt event_ie prnt_entr if event_ie==-1 & prnt_entr==1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_prnt: gen ie_prnt_peyes_nobs = _N
collapse ie_prnt_peyes_nobs, by(ptile_prnt)
rename ptile_prnt rank
save "$results\03_selection\prnt_ie_peyes.dta", replace

use ptile_prnt event_ie prnt_entr if event_ie==-1 & prnt_entr==0 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_prnt: gen ie_prnt_peno_nobs = _N
collapse ie_prnt_peno_nobs, by(ptile_prnt)
rename ptile_prnt rank
save "$results\03_selection\prnt_ie_peno.dta", replace

* Combine

use "$results\03_selection\own_ie_peyes.dta", clear
merge 1:1 rank using "$results\03_selection\own_ie_peno.dta"
drop _merge
merge 1:1 rank using "$results\03_selection\prnt_ie_peyes.dta"
drop _merge
merge 1:1 rank using "$results\03_selection\prnt_ie_peno.dta"
drop _merge
erase "$results\03_selection\own_ie_peyes.dta"
erase "$results\03_selection\own_ie_peno.dta"
erase "$results\03_selection\prnt_ie_peyes.dta"
erase "$results\03_selection\prnt_ie_peno.dta"

foreach x in ie_own_peyes ie_own_peno ie_prnt_peyes ie_prnt_peno {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_own_peyes_nobs "Number of new inc. entrepreneurs w/ entrep. parents by income rank at t-1"
label var ie_own_peno_nobs "Number of new inc. entrepreneurs w/o entrep. parents by income rank at t-1"
label var ie_prnt_peyes_nobs "Number of new inc. entrepreneurs w/ entrep. parents by parental income rank"
label var ie_prnt_peno_nobs "Number of new inc. entrepreneurs w/o entrep. parents by parental income rank"
label var ie_own_peyes_shr "Share of new inc. entrepreneurs w/ entrep. parents by income rank at t-1"
label var ie_own_peno_shr "Share of new inc. entrepreneurs w/o entrep. parents by income rank at t-1"
label var ie_prnt_peyes_shr "Share of new inc. entrepreneurs w/ entrep. parents by parental income rank"
label var ie_prnt_peno_shr "Share of new inc. entrepreneurs w/o entrep. parents by parental income rank"
label var rank "Percentile rank, either at t-1 or parental"

save "$results\03_selection\fig2.dta", replace

/*
________________________________________________________________________________
Figure 3: Initial equity of new incorporated businesses by individual and parental income and by parental entrepreneurship
*/

use shnro vuosi event_ie tol2dig prnt_entr ptile_dspi ptile_prnt if event_ie==0 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\01_main\folk_flown_merge_eventready_iespec.dta", keep(3) keepusing(eq)
replace eq=1 if eq<1 | eq==.
gen log_eq = log(eq)
drop shnro vuosi event_ie _merge

* Decile ranks
egen dile_dspi = cut(ptile_dspi), at(1(10)101)
egen dile_prnt = cut(ptile_prnt), at(1(10)101)

cd "$results\03_selection"
binsreg log_eq dile_dspi i.tol2dig if prnt_entr==1, ci(T) cb(T) nbins(10) savedata(eq_own_peyes) replace
binsreg log_eq dile_dspi i.tol2dig if prnt_entr==0, ci(T) cb(T) nbins(10) savedata(eq_own_peno) replace
binsreg log_eq dile_prnt i.tol2dig if prnt_entr==1, ci(T) cb(T) nbins(10) savedata(eq_prnt_peyes) replace
binsreg log_eq dile_prnt i.tol2dig if prnt_entr==0, ci(T) cb(T) nbins(10) savedata(eq_prnt_peno) replace
gr drop _all

use dots_x dots_fit CI_l CI_r using eq_own_peyes, clear
rename dots_fit eq_own_peyes
rename CI_l eq_own_peyes_95lo
rename CI_r eq_own_peyes_95up
merge 1:1 dots_x using eq_own_peno
drop _merge
rename dots_fit eq_own_peno
rename CI_l eq_own_peno_95lo
rename CI_r eq_own_peno_95up
merge 1:1 dots_x using eq_prnt_peyes
drop _merge
rename dots_fit eq_prnt_peyes
rename CI_l eq_prnt_peyes_95lo
rename CI_r eq_prnt_peyes_95up
merge 1:1 dots_x using eq_prnt_peno
drop _merge
rename dots_fit eq_prnt_peno
rename CI_l eq_prnt_peno_95lo
rename CI_r eq_prnt_peno_95up
gen decile=_n
order decile
keep decile eq*
rename eq_* *

erase eq_own_peyes.dta
erase eq_own_peno.dta
erase eq_prnt_peyes.dta
erase eq_prnt_peno.dta

label var decile "Decile rank, either at t-1 or parental"
label var own_peyes "Average log initial equity by own rank among those w/ entrep. parents"
label var own_peno "Average log initial equity by own rank among those w/o entrep. parents"
label var prnt_peyes "Average log initial equity by parental rank among those w/ entrep. parents"
label var prnt_peno "Average log initial equity by parental rank among those w/o entrep. parents"

save "$results\03_selection\fig3.dta", replace









/*
________________________________________________________________________________
Appendix
*/

/*
________________________________________________________________________________
Table A2: Probability of starting an incorporated business: Comparing the relative importance of various predictors using dominance analysis
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear

* Restriction to have a common sample across the analysis: have to observe from t-1 to t+10
keep if event_ie==-1 | event_ie==10 | event_we==-1 | event_we==10

* Dummy for unemployment at t-1
gen unempl=0
replace unempl=1 if (tyotur_dummy==1) & (event_ie==-1 | event_uie==-1 | event_we==-1)

* Dummy for studying at t-1
gen student=0
replace student=1 if (opira_dummy==1) & (event_ie==-1 | event_uie==-1 | event_we==-1)

******************************************

/*
Education more granularly
*/

* First, save our t-1 tags to be used when getting addtional data
preserve
keep if event_ie==-1 | event_we==-1
keep shnro vuosi
save "$data\99_temp\tm1tags.dta", replace
restore

* Second, education level and field
preserve
use vuosi shnro ututku_aste ututku_ala if vuosi>=1997 using "D:\ready-made\FOLK_perus_8800a\folk_19872000_tua_perus20tot_2.dta", clear
append using "D:\ready-made\FOLK_perus_0110a\folk_20012010_tua_perus20tot_2.dta", keep(vuosi shnro ututku_aste ututku_ala)
append using "D:\ready-made\FOLK_perus_11a\folk_20112019_tua_perus20tot_2.dta", keep(vuosi shnro ututku_aste ututku_ala)
merge 1:1 shnro vuosi using "$data\99_temp\tm1tags.dta", keep(3)
destring ututku_aste, replace
destring ututku_ala, replace
keep if inrange(vuosi,1997,2013)
drop _merge
save "$data\99_temp\ututku.dta", replace
restore
merge 1:1 shnro vuosi using "$data\99_temp\ututku.dta"
drop _merge

* We now have it for t-1. Extend for all years
rename ututku_aste ututku_aste_m1
rename ututku_ala ututku_ala_m1
bysort shnro: egen ututku_aste = max(ututku_aste_m1)
bysort shnro: egen ututku_ala = max(ututku_ala_m1)
replace ututku_aste=0 if ututku_aste==.
replace ututku_ala=0 if ututku_ala==.
drop *_m1

erase "$data\99_temp\tm1tags.dta"
erase "$data\99_temp\ututku.dta"

************************************

/*
Dominance analysis
*/

* Define the outcome
preserve
keep if event_ie==-1 | event_we==-1
gen strt_ie=0
replace strt_ie=1 if event_ie==-1
* Define interactions ('domin' does not support i.var1#i.var2)
rename ika age
rename ikasq age2
rename sukup sex
gen age_x_sex=age*sex
gen age2_x_sex=age2*sex
gen age_x_ututku_aste=age*ututku_aste
gen age2_x_ututku_aste=age2*ututku_aste
gen age_x_ptile_dspi=age*ptile_dspi
gen age2_x_ptile_dspi=age2*ptile_dspi
gen age_x_ptile_prnt=age*ptile_prnt
gen age2_x_ptile_prnt=age2*ptile_prnt
gen age_x_prnt_entr=age*prnt_entr
gen age2_x_prnt_entr=age2*prnt_entr
gen sex_x_ututku_aste=sex*ututku_aste
gen sex_x_ptile_dspi=sex*ptile_dspi
gen sex_x_ptile_prnt=sex*ptile_prnt
gen sex_x_prnt_entr=sex*prnt_entr
gen ututku_aste_x_ptile_dspi=ututku_aste*ptile_dspi
gen ututku_aste_x_ptile_prnt=ututku_aste*ptile_prnt
gen ututku_aste_x_prnt_entr=ututku_aste*prnt_entr
gen ptile_dspi_x_ptile_prnt=ptile_dspi*ptile_prnt
gen ptile_dspi_x_prnt_entr=ptile_dspi*prnt_entr
gen ptile_prnt_x_prnt_entr=ptile_prnt*prnt_entr
* The analysis
domin strt_ie prnt_entr uie sex pks married unempl student, /// simple dummies
sets((i.ptile_dspi) (i.ptile_prnt) (i.tol2dig) (i.ututku_aste) (i.ututku_ala) (age age2) /// simple sets
(age_x_ptile_dspi age2_x_ptile_dspi sex_x_ptile_dspi ututku_aste_x_ptile_dspi /// own rank interactions
age_x_ptile_prnt age2_x_ptile_prnt sex_x_ptile_prnt ututku_aste_x_ptile_prnt /// parent rank interactions
age_x_prnt_entr age2_x_prnt_entr sex_x_prnt_entr ututku_aste_x_prnt_entr /// parent entrep. interactions
ptile_dspi_x_ptile_prnt ptile_dspi_x_prnt_entr ptile_prnt_x_prnt_entr) /// rank & parent entrep. interactions
(age_x_sex age2_x_sex age_x_ututku_aste age2_x_ututku_aste sex_x_ututku_aste)) // Mincerian interactions
restore

* Directory
cd "$results\03_selection"

* Save dominance statistics
preserve
matrix b=e(b)
gen nr=_n
gen domstat=.
forval i=1(1)15{
	replace domstat=b[1,`i'] if nr==`i'
}
replace domstat=e(N) if nr==16
replace domstat=e(fitstat_o) if nr==17
keep if nr<=17
keep nr domstat
save domstat, replace
restore

* Save standardized dominance statistics
preserve
matrix b=e(std)
gen nr=_n
gen stddomstat=.
forval i=1(1)15{
	replace stddomstat=b[1,`i'] if nr==`i'
}
keep if nr<=15
keep nr stddomstat
save stddomstat, replace
restore

* Save ranking
preserve
matrix b=e(ranking)
gen nr=_n
gen ranking=.
forval i=1(1)15{
	replace ranking=b[1,`i'] if nr==`i'
}
keep if nr<=15
keep nr ranking
save ranking, replace
restore

* Merge the above results into single file
use domstat, clear
merge 1:1 nr using stddomstat
drop _merge
merge 1:1 nr using ranking
drop _merge
gen predictor=""
replace predictor="Parent entrepreneur" if nr==1
replace predictor="Unincorporated at t-1" if nr==2
replace predictor="Gender" if nr==3
replace predictor="Capital region at t-1" if nr==4
replace predictor="Married at t-1" if nr==5
replace predictor="Unemployed at t-1" if nr==6
replace predictor="Student at t-1" if nr==7
replace predictor="Individual rank at t-1" if nr==8
replace predictor="Parental rank" if nr==9
replace predictor="Industry at t-1" if nr==10
replace predictor="Level of highest degree at t-1" if nr==11
replace predictor="Field of highest degree at t-1" if nr==12
replace predictor="Age (linear and quadratic)" if nr==13
replace predictor="Mincerian interactions" if nr==14
replace predictor="Other interactions" if nr==15
replace predictor="Number of observations" if nr==16
replace predictor="Total R2 of the full model" if nr==17
order predictor

save tabA2, replace
erase domstat.dta
erase stddomstat.dta
erase ranking.dta

/*
________________________________________________________________________________
Figure A2: Share of new entrepreneurs by age
*/

use ika event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ika: gen ie_age_nobs = _N
collapse ie_age_nobs, by(ika)
save "$results\03_selection\age_ie.dta", replace

use ika event_uie if event_uie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ika: gen uie_age_nobs = _N
collapse uie_age_nobs, by(ika)
save "$results\03_selection\age_uie.dta", replace

* Combine

use "$results\03_selection\age_ie.dta", clear
merge 1:1 ika using "$results\03_selection\age_uie.dta"
drop _merge
erase "$results\03_selection\age_ie.dta"
erase "$results\03_selection\age_uie.dta"

foreach x in ie_age uie_age {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_age_nobs "Number of new incorporated entrepreneurs by age"
label var uie_age_nobs "Number of new unincorporated entrepreneurs by age"
label var ie_age_shr "Share of new incorporated entrepreneurs by age"
label var uie_age_shr "Share of new unincorporated entrepreneurs by age"

drop if ika>56
rename ika age

save "$results\03_selection\figA2.dta", replace

/*
________________________________________________________________________________
Figure A3: Share of new entrepreneurs by education
*/

/*
Education more granularly
*/

* First, save our t-1 tags to be used when getting addtional data
use shnro vuosi event_ie event_uie event_we using "$data\01_main\folk_flown_merge_eventready.dta", clear
keep if event_ie==-1 | event_uie==-1 | event_we==-1
keep shnro vuosi
save "$data\99_temp\tm1tags.dta", replace

* Second, education level and field
use vuosi shnro ututku_aste ututku_ala if vuosi>=1997 using "D:\ready-made\FOLK_perus_8800a\folk_19872000_tua_perus20tot_2.dta", clear
append using "D:\ready-made\FOLK_perus_0110a\folk_20012010_tua_perus20tot_2.dta", keep(vuosi shnro ututku_aste ututku_ala)
append using "D:\ready-made\FOLK_perus_11a\folk_20112019_tua_perus20tot_2.dta", keep(vuosi shnro ututku_aste ututku_ala)
merge 1:1 shnro vuosi using "$data\99_temp\tm1tags.dta", keep(3)
destring ututku_aste, replace
destring ututku_ala, replace
keep if inrange(vuosi,1997,2013)
drop _merge
save "$data\99_temp\ututku.dta", replace

* Level of education

use shnro vuosi event_uie if event_uie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\ututku.dta", keep(3)
drop _merge
bysort ututku_aste: gen uie_edul_nobs = _N
collapse uie_edul_nobs, by(ututku_aste)
save "$results\03_selection\edul_uie.dta", replace

use shnro vuosi event_we if event_we==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\ututku.dta", keep(3)
drop _merge
bysort ututku_aste: gen we_edul_nobs = _N
collapse we_edul_nobs, by(ututku_aste)
save "$results\03_selection\edul_we.dta", replace

use shnro vuosi event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\ututku.dta", keep(3)
drop _merge
bysort ututku_aste: gen ie_edul_nobs = _N
collapse ie_edul_nobs, by(ututku_aste)

merge 1:1 ututku_aste using "$results\03_selection\edul_uie.dta"
drop _merge
merge 1:1 ututku_aste using "$results\03_selection\edul_we.dta"
drop _merge

foreach x in ie_edul uie_edul we_edul {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_edul_nobs "Number of new incorporated entrepreneurs by education level"
label var uie_edul_nobs "Number of new unincorporated entrepreneurs by education level"
label var we_edul_nobs "Number of pseudo-starters by education level"
label var ie_edul_shr "Share of new incorporated entrepreneurs by education level"
label var uie_edul_shr "Share of new unincorporated entrepreneurs by education level"
label var we_edul_shr "Share of pseudo-starters by education level"
label var ututku_aste "Education level"

save "$results\03_selection\figA3_top.dta", replace

* Field of education

use shnro vuosi event_uie if event_uie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\ututku.dta", keep(3)
drop _merge
bysort ututku_ala: gen uie_eduf_nobs = _N
collapse uie_eduf_nobs, by(ututku_ala)
save "$results\03_selection\eduf_uie.dta", replace

use shnro vuosi event_we if event_we==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\ututku.dta", keep(3)
drop _merge
bysort ututku_ala: gen we_eduf_nobs = _N
collapse we_eduf_nobs, by(ututku_ala)
save "$results\03_selection\eduf_we.dta", replace

use shnro vuosi event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\ututku.dta", keep(3)
drop _merge
bysort ututku_ala: gen ie_eduf_nobs = _N
collapse ie_eduf_nobs, by(ututku_ala)

merge 1:1 ututku_ala using "$results\03_selection\eduf_uie.dta"
drop _merge
merge 1:1 ututku_ala using "$results\03_selection\eduf_we.dta"
drop _merge

foreach x in ie_eduf uie_eduf we_eduf {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_eduf_nobs "Number of new incorporated entrepreneurs by education level"
label var uie_eduf_nobs "Number of new unincorporated entrepreneurs by education level"
label var we_eduf_nobs "Number of pseudo-starters by education level"
label var ie_eduf_shr "Share of new incorporated entrepreneurs by education level"
label var uie_eduf_shr "Share of new unincorporated entrepreneurs by education level"
label var we_eduf_shr "Share of pseudo-starters by education level"
label var ututku_ala "Education field"

save "$results\03_selection\figA3_bottom.dta", replace

* Erase

erase "$results\03_selection\edul_uie.dta"
erase "$results\03_selection\edul_we.dta"
erase "$results\03_selection\eduf_uie.dta"
erase "$results\03_selection\eduf_we.dta"
erase "$data\99_temp\tm1tags.dta"
erase "$data\99_temp\ututku.dta"

/*
________________________________________________________________________________
Figure A4: Share of highly-educated individuals by individual income
*/

use shnro vuosi highed ptile_dspi event_uie if event_uie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_dspi: gen uie_he_nobs = _N
collapse uie_he_nobs, by(ptile_dspi)
save "$results\03_selection\he_uie.dta", replace

use shnro vuosi highed ptile_dspi event_we if event_we==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_dspi: gen we_he_nobs = _N
collapse we_he_nobs, by(ptile_dspi)
save "$results\03_selection\he_we.dta", replace

use shnro vuosi highed ptile_dspi event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_dspi: gen ie_he_nobs = _N
collapse ie_he_nobs, by(ptile_dspi)

merge 1:1 ptile_dspi using "$results\03_selection\he_uie.dta"
drop _merge
merge 1:1 ptile_dspi using "$results\03_selection\he_we.dta"
drop _merge

foreach x in ie_he uie_he we_he {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_he_nobs "Number of highly-educated new incorporated entrepreneurs by t-1 income rank"
label var uie_he_nobs "Number of highly-educated new unincorporated entrepreneurs by t-1 income rank"
label var we_he_nobs "Number of highly-educated pseudo-starters by t-1 income rank"
label var ie_he_shr "Share of highly-educated new incorporated entrepreneurs by t-1 income rank"
label var uie_he_shr "Share of highly-educated new unincorporated entrepreneurs by t-1 income rank"
label var we_he_shr "Share of highly-educated pseudo-starters by t-1 income rank"
rename ptile_dspi rank
label var rank "Percentile rank at t-1"

save "$results\03_selection\figA4.dta", replace

erase "$results\03_selection\he_uie.dta"
erase "$results\03_selection\he_we.dta"

/*
________________________________________________________________________________
Figure A6: Share of new incorporated entrepreneurs by parental income using different income concepts
*/

use shnro event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro using "$data\02_parents\ptile_parent_capinc.dta", keep(3) keepusing(ptile_ci)
bysort ptile_ci: gen ie_prnt_ci_nobs = _N
collapse ie_prnt_ci_nobs, by(ptile_ci)
rename ptile_ci rank
save "$results\03_selection\ci.dta", replace

use shnro event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro using "$data\02_parents\ptile_parent_diinc.dta", keep(3) keepusing(ptile_di)
bysort ptile_di: gen ie_prnt_di_nobs = _N
collapse ie_prnt_di_nobs, by(ptile_di)
rename ptile_di rank
save "$results\03_selection\di.dta", replace

use ptile_prnt event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_prnt: gen ie_prnt_mi_nobs = _N
collapse ie_prnt_mi_nobs, by(ptile_prnt)
rename ptile_prnt rank

merge 1:1 rank using "$results\03_selection\ci.dta"
drop _merge
merge 1:1 rank using "$results\03_selection\di.dta"
drop _merge

foreach x in ie_prnt_mi ie_prnt_ci ie_prnt_di {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_prnt_mi_nobs "Number of new incorporated entrepreneurs by parental market income rank"
label var ie_prnt_ci_nobs "Number of new incorporated entrepreneurs by parental capital income rank"
label var ie_prnt_di_nobs "Number of new incorporated entrepreneurs by parental disposable income rank"
label var ie_prnt_mi_shr "Share of new incorporated entrepreneurs by parental market income rank"
label var ie_prnt_ci_shr "Share of new incorporated entrepreneurs by parental capital income rank"
label var ie_prnt_di_shr "Share of new incorporated entrepreneurs by parental disposable income rank"
label var rank "Parental rank, either market, capital or disposable income"

save "$results\03_selection\figA6.dta", replace

erase "$results\03_selection\ci.dta"
erase "$results\03_selection\di.dta"

/*
________________________________________________________________________________
Figure A7: Share of new entrepreneurs by parental income: New firms vs. existing firms
*/

/*
Entrepreneurs in general by parental rank
*/

use shnro vuosi ie uie if ie==1 | uie==1 using "$data\01_main\folk_flown_merge.dta", clear
merge m:1 shnro using "$data\02_parents\ptile_parent_capinc.dta", keepusing(ptile) keep(3)
drop _merge

preserve
keep if ie==1
keep if vuosi==2006
bysort ptile: gen ie06_nobs = _N
collapse ie06_nobs, by(ptile)
save "$results\03_selection\alliebyprntrank_06.dta", replace
restore

preserve
keep if uie==1
keep if vuosi==2006
bysort ptile: gen uie06_nobs = _N
collapse uie06_nobs, by(ptile)
save "$results\03_selection\alluiebyprntrank_06.dta", replace
restore

preserve
keep if ie==1
keep if vuosi==2014
bysort ptile: gen ie14_nobs = _N
collapse ie14_nobs, by(ptile)
save "$results\03_selection\alliebyprntrank_14.dta", replace
restore

preserve
keep if uie==1
keep if vuosi==2014
bysort ptile: gen uie14_nobs = _N
collapse uie14_nobs, by(ptile)
save "$results\03_selection\alluiebyprntrank_14.dta", replace
restore

/*
Those who join existing firms with no prior ownership
*/

use if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
keep shnro vuosi ie uie event_ie id
save "$data\99_temp\new_ie_firm.dta", replace
merge m:1 shnro using "$data\02_parents\ptile_parent_capinc.dta", keepusing(ptile) keep(3)
drop _merge
use if new_ie==1 using "$data\01_main\folk_flown_merge.dta", clear
keep shnro vuosi ie uie id
merge m:1 shnro using "$data\99_temp\new_ie_firm.dta"
erase "$data\99_temp\new_ie_firm.dta"
ren _merge MERGE
gen new_ie_new_firm=1 if event_ie==-1
replace new_ie_new_firm=0 if new_ie_new_firm==.
merge m:1 shnro using "$data\02_parents\ptile_parent_capinc.dta", keepusing(ptile) keep(3)
drop _merge
bysort ptile new_ie_new_firm: gen newie_firm_byprntrank = _N
collapse newie_firm_byprntrank, by(ptile new_ie_new_firm)
rename newie_firm_byprntrank newie_oldfirm_nobs
keep if new_ie_new_firm==0
drop new_ie_new_firm
save "$results\03_selection\newie_oldfirm_byprntrank.dta", replace

/*
UIE baseline
*/

use ptile_prnt event_uie if event_uie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_prnt: gen uie_prnt_nobs = _N
collapse uie_prnt_nobs, by(ptile_prnt)
save "$results\03_selection\prnt_uie.dta", replace

/*
IE baseline and merge everything else
*/

use ptile_prnt event_ie if event_ie==-1 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort ptile_prnt: gen ie_prnt_nobs = _N
collapse ie_prnt_nobs, by(ptile_prnt)

merge 1:1 ptile_prnt using "$results\03_selection\prnt_uie.dta"
drop _merge
rename ptile_prnt ptile
merge 1:1 ptile using "$results\03_selection\alliebyprntrank_06.dta"
drop _merge
merge 1:1 ptile using "$results\03_selection\alluiebyprntrank_06.dta"
drop _merge
merge 1:1 ptile using "$results\03_selection\alliebyprntrank_14.dta"
drop _merge
merge 1:1 ptile using "$results\03_selection\alluiebyprntrank_14.dta"
drop _merge
merge 1:1 ptile using "$results\03_selection\newie_oldfirm_byprntrank.dta"
drop _merge
rename ptile rank
label var rank "Parental income rank"

foreach x in ie_prnt uie_prnt ie06 uie06 ie14 uie14 newie_oldfirm {
	egen `x'_ttl = sum(`x'_nobs)
	gen `x'_shr = (`x'_nobs / `x'_ttl) * 100
}
drop *ttl

label var ie_prnt_nobs "Number of new inc. entrepreneurs who establish new firms"
label var uie_prnt_nobs "Number of new uninc. entrepreneurs who establish new firms"
label var ie06_nobs "Number of all inc. entrepreneurs in 2006"
label var uie06_nobs "Number of all uninc. entrepreneurs in 2006"
label var ie14_nobs "Number of all inc. entrepreneurs in 2014"
label var uie14_nobs "Number of all uninc. entrepreneurs in 2014"
label var newie_oldfirm_nobs "Number of new inc. entrepreneurs who join existing firms"
label var ie_prnt_shr "Share of new inc. entrepreneurs who establish new firms"
label var uie_prnt_shr "Share of new uninc. entrepreneurs who establish new firms"
label var ie06_shr "Share of all inc. entrepreneurs in 2006"
label var uie06_shr "Share of all uninc. entrepreneurs in 2006"
label var ie14_shr "Share of all inc. entrepreneurs in 2014"
label var uie14_shr "Share of all uninc. entrepreneurs in 2014"
label var newie_oldfirm_shr "Share of new inc. entrepreneurs who join existing firms"

save "$results\03_selection\figA7.dta", replace

erase "$results\03_selection\prnt_uie.dta"
erase "$results\03_selection\newie_oldfirm_byprntrank.dta"
erase "$results\03_selection\alliebyprntrank_06.dta"
erase "$results\03_selection\alliebyprntrank_14.dta"
erase "$results\03_selection\alluiebyprntrank_06.dta"
erase "$results\03_selection\alluiebyprntrank_14.dta"
